<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "site_config".
 *
 * @property int $id 自增主键
 * @property string $name 变量名
 * @property string $description 变量名名描述
 * @property string $form_type 变量数据类型
 * @property string $value 变量值
 * @property int $deleted 是否删除，0：未删除；1：已删除
 * @property string $created 创建时间
 * @property string $modified 修改时间
 */
class SiteConfig extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'site_config';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'description', 'form_type'], 'required'],
            [['deleted'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['name'], 'string', 'max' => 50],
            [['description'], 'string', 'max' => 100],
            [['form_type'], 'string', 'max' => 20],
            [['value'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '自增主键'),
            'name' => Yii::t('app', '变量名'),
            'description' => Yii::t('app', '变量名名描述'),
            'form_type' => Yii::t('app', '变量数据类型'),
            'value' => Yii::t('app', '变量值'),
            'deleted' => Yii::t('app', '是否删除，0：未删除；1：已删除'),
            'created' => Yii::t('app', '创建时间'),
            'modified' => Yii::t('app', '修改时间'),
        ];
    }


    public function getConfigList($orm = '', $field = '*')
    {
        $orm = $this->getOrm($orm)->andWhere(['deleted' => 0]);
        $result = $orm->select($field)->asArray()->all();
        return $result;
    }


    /**
     * 获取配置信息byID
     * @access public
     * @author furong
     * @param $id
     * @param string $field
     * @return bool|\idiorm\orm\ORM
     * @since 2017年6月22日 10:49:01
     * @abstract
     */
    public function getConfigInfoById($id, $field = '*')
    {
        $condition = [
            'where' => ['id', $id]
        ];
        return $this->getConfigInfo($condition, $field);
    }

    public function updateConfig($orm, $data)
    {
        $orm = $this->getOrm($orm);
        $result = $orm->one();
        if (!$result) {
            $this->setMessage('配置项不存在');
            return false;
        }
        $data['modified'] = getDateTime();
        $result->setAttributes($data);
        if ($result->update(false)) {
            return true;
        } else {
            $this->setMessage('更新配置项失败');
            return false;
        }
    }

    /**
     * 删除配置项
     * @access public
     * @author furong
     * @param $id
     * @return bool
     * @since 2017年6月22日 10:19:20
     * @abstract
     */
    public function delConfig($id)
    {
        $condition = [
            'where' => ['id', $id]
        ];
        $data = [
            'deleted' => self::DELETED,
        ];
        $result = $this->updateConfig($condition, $data);
        if ($result) {
            $this->setMessage('配置删除成功');
        } else {
            $this->setMessage('配置删除失败');
        }
        return $result;
    }
}
